package com.forum.controller;

import cn.undraw.util.result.R;
import com.forum.common.JwtUtils;
import com.forum.dto.NoteDTO;
import com.forum.entity.Result1;
import com.forum.service.NoteService;
import com.forum.vo.NoteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/note")
@Slf4j
@CrossOrigin  //允许跨域请求
@Api(tags = "文章相关接口")
public class NoteController {

    @Autowired
    private NoteService noteService;

    //添加文章
    @ApiOperation("添加文章")
    @PostMapping("/add")
    public Result1 addNote(ServletRequest request, @RequestBody NoteDTO noteDTO) {
        HttpServletRequest req = (HttpServletRequest) request;
        String jwt = req.getHeader("token");
        JwtUtils.parseJWT(jwt);
        log.info("用户传进来的note:{}", noteDTO);
        return noteService.addNote(noteDTO);
    }

    //查询文章,可根据文章id，用户id，文章类型,动态查询
    @GetMapping("/findNote")
    @ApiOperation("查询文章")
    public Result1 findNote(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) LocalDateTime updateTime,
            @RequestParam(required = false) String noteTitle,
            @RequestParam(defaultValue = "1") int page,  //第几页
            @RequestParam(defaultValue = "5") int pageSize,  //每页几条数据
            @RequestParam(defaultValue = "0") int orderByLike //是否按照点赞数排序 0否 1是
            ) {
        log.info("用户传进来的查询文章参数:{},{},{}", id, userId, type);
        //msgNeed 中0表示不需要,1表示需要msg
        List<NoteVO> noteVOList = noteService.findNote(id, userId, type, updateTime, noteTitle, page, pageSize,orderByLike);
        return Result1.success(noteVOList);
    }

    //修改文章，需要文章dto对象
    @PostMapping("/update")
    @ApiOperation("修改文章")
    public Result1 update(@RequestBody NoteDTO noteDTO) {
        log.info("修改信息：{}", noteDTO);
        return noteService.update(noteDTO);
    }

    //删除文章，需要文章id
    @DeleteMapping("/delnote")
    @ApiOperation("删除文章")
    public Result1 delNote(Integer id) {
        log.info("需要删除的文章id:{}", id);
        return noteService.delNote(id);
    }

    //查询该用户总文章数
    @GetMapping("/getNoteNumByUserId")
    @ApiOperation("查询该用户总文章数")
    public Result1 getNoteNumByUserId(Integer userId) {
        int num = noteService.getNoteNumByUserId(userId);
        return Result1.success(num);
    }

    //查看当前用户是否对文章进行过点赞收藏以及关注发布文章用户
    //TODO:判断不是自己才不用关注，给个提示：不能关注自己
    @PostMapping("/seleUserByLCC")
    @ApiOperation("查看当前用户是否对文章进行过点赞收藏以及关注发布文章用户")
    public R seleUserByLCC(Integer noteId) {
        return noteService.seleUserByLCC(noteId);
    }
}
